package xyz.suancaiyu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import xyz.suancaiyu.dao.BookDao;
import xyz.suancaiyu.pojo.Book;

/**
 * Spring Boot + Spring WebFlux + Thymeleaf + H2(内存数据库) 示例项目
 *
 * @author puruidong
 * @version 2017/10/21 1.0
 */
@Controller
public class HomeController {

    @Autowired
    private BookDao bookDao;

    @GetMapping("/")
    public String home(Model model) {
        model.addAttribute("infolist", bookDao.list());
        model.addAttribute("info", " Welcome ! ");
        return "home";
    }

    @PostMapping("/saveTodo")
    public String saveTodo(@ModelAttribute("todo") Book todo) {
        if (null != todo && StringUtils.hasLength(todo.getTodo())) {
            bookDao.save(todo.getTodo());
        }
        return "redirect:/";
    }

    @GetMapping("/delete")
    public String delete(String id) {
        bookDao.delete(id);
        return "redirect:/";
    }

}
